package com.novax.ex.activity.provider;

import com.novax.ex.common.constant.StatusCode;
import com.novax.ex.common.exception.BusinessException;
import com.novax.ex.common.results.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Description: 全局的的异常拦截器
 *
 * @author shaw
 * @date 2018/12/27 10:46
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ReturnResult businessException(BusinessException e) {
        log.warn("业务逻辑异常={}", e.getMessage(), e);
        return new ReturnResult(StatusCode.ERROR, e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ReturnResult globalException(Exception e) {
        log.warn("程序异常={}", e.getMessage(), e);
        return new ReturnResult(StatusCode.EXCEPTION, "Internal Server Error");
    }
}
